package cn.seaboot.admin.quartz.smart;

import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 自销毁计划任务
 * <p>
 * 只触发一次的任务，触发之后立即移除任务。
 * 数据库不会存储任何相关信息，需要业务自身控制执行逻辑。
 *
 * @author Mr.css
 * @version 2019/12/7 10:19
 */
public abstract class OnceJob implements Job {
    private final Logger logger = LoggerFactory.getLogger(OnceJob.class);

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        JobDetail detail = jobExecutionContext.getJobDetail();
        try {
            this.handler(detail.getJobDataMap());
        } finally {
            try {
                // when handler finish，remove this job
                Scheduler scheduler = jobExecutionContext.getScheduler();
                scheduler.deleteJob(detail.getKey());
            } catch (SchedulerException e) {
                logger.error("destroy once-job failed：", e);
            }
        }
    }

    /**
     * 事件回调，启动定时任务的时候，可以通过JobDataMap map进行传值
     *
     * @param map JobDataMap
     */
    public abstract void handler(JobDataMap map);
}
